package nu.board.service;

import java.io.File;
import java.io.IOException;
import java.util.List;

import nu.board.model.BoardDao;
import nu.board.model.BoardVO;
import nu.board.model.CategoryVO;
import nu.board.model.ConfirmVO;
import nu.board.model.GroupVO;
import nu.board.model.PlanVO;
import nu.board.model.ReplyVO;
import nu.paging.ListVO;
import nu.paging.PagingBean;

import org.springframework.web.multipart.MultipartFile;

public class BoardServiceImpl implements BoardService{
	private BoardDao boardDao;
	//private String path="C:\\java-kosta\\workspace\\mediaproject\\nu\\WebContent\\uploadFileForBoard\\"; // 테스트용 workspace file path
	private String path = "C:\\java-kosta\\servers\\mediaproject-tomcat\\webapps\\nu\\uploadFileForBoard\\"; // 실제 서버 file path
	
	public BoardServiceImpl(BoardDao boardDao){
		this.boardDao = boardDao;
	}
	/**
	 * 그룹들의 리스트를 담아 오는 메소드
	 */
	@Override
	public List<GroupVO> showGroupList(String category) throws Exception {
		return boardDao.selectGroupList(category);
	}
	
	/**
	 * 특정 그룹이 쓴 글만 가져와서 페이징 하는 메소드
	 */
	@Override
	public ListVO getPostingList(CategoryVO cvo) throws Exception {
		List<BoardVO> list = boardDao.selectPostingList(cvo);
		int total = boardDao.selectTotalPostingCount(cvo);
		PagingBean pb = new PagingBean(total, cvo.getPageNo());
		List<PlanVO> planVOlist=boardDao.selectPlanListByGroupId(cvo.getGroup_id());
		return new ListVO(list,pb,planVOlist);
	}
	
	/**
	 * 글 내용을 보여주기 위해 내용을 가져오는 메소드
	 * 글 보여주기 전에 hits수를 1 더한다.
	 */
	@Override
	public BoardVO getContent(int no) throws Exception {
		boardDao.updateHits(no);
		BoardVO bvo= boardDao.selectContent(no);
		bvo.setConfirmedIdList(boardDao.selectConfirmedIdListByBoardNo(no));
		return bvo;
	}
	/**
	 * 댓글을 입력하는 메소드
	 */
	@Override
	public int registerReply(ReplyVO rvo) throws Exception {
		return boardDao.insertReply(rvo);
	}
	/**
	 * 해당 board 번호에 있는 모든 댓글 list를 받아오는 메소드
	 */
	@Override
	public List getReplyListByBoardNo(int no) throws Exception {
		return boardDao.selectReplyListByBoardNo(no);
	}
	/**
	 * 게시글 작성하기
	 */
	@Override
	public BoardVO writeContent(BoardVO vo) throws Exception {
		
		if(!vo.getUploadFileForBoard().isEmpty()){
			MultipartFile mFile=vo.getUploadFileForBoard();
			vo.setOrgfilename(mFile.getOriginalFilename());
			vo.setSavefilename(System.currentTimeMillis()+"_"+mFile.getOriginalFilename());
		}else{
			vo.setOrgfilename("default.png");
			vo.setSavefilename("default.png");
		}
		System.out.println(vo.getOrgfilename()+"   "+vo.getSavefilename());
		
		int result=boardDao.insertContent(vo);
		
		if(result!=0&&!vo.getUploadFileForBoard().isEmpty()){
			MultipartFile mFile=vo.getUploadFileForBoard();
			// 업로드할 원본 파일을 이동할 파일 객체를 생성
			File copyFile = new File(path+vo.getSavefilename());
			try {
				mFile.transferTo(copyFile);
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
		}
		return vo;
	}
	@Override
	public GroupVO registerGroup(GroupVO gvo) throws Exception {
		 int result=boardDao.insertGroup(gvo);
		 return gvo;
	}
	@Override
	public void confirmedBoard(ConfirmVO cfvo) throws Exception {
		boardDao.insertConfirmedBoardNoAndMemberId(cfvo);
		boardDao.updateConfirmationPlus(cfvo.getNo());
	}
	@Override
	public void insertPlan(PlanVO pvo) throws Exception {
		boardDao.insertPlan(pvo);
	}
	@Override
	public PlanVO showPlanDetail(int nu_plan_no) throws Exception {
		return boardDao.selectPlanByPlanNo(nu_plan_no);
	}
	
}
